package cn.jiaxu.musicplayer2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

public class MyService extends Service {
    private int progress=0;
    private boolean flag = false;
    public MyService() {
    }
    public class MyBinder extends Binder{
       public int getProgress() {
           return progress;
       }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        int action =intent.getIntExtra("service", -1);
        switch(action) {
            case 1:
                play();
                break;
            case 2:
                pause();
                break;
            case 3:
                resume();
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }
    public void play() {
        Thread th = new Thread(){
            @Override
            public void run() {
                synchronized (MyService.this) {
                    for (int i = 0; i <= 600; i++) {
                        if(flag) {
                            try {
                                MyService.this.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        progress++;
                    }
                }
            }
        };
        th.start();
    }
    public void pause() {
        flag = true;
    }
    public synchronized void resume() {
        flag=false;
        notify();
    }

    @Override
    public IBinder onBind(Intent intent) {
       return new MyBinder();
    }
}
